package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigWeightDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.PigWeight;

import cn.edu.scau.cmi.hombio.service.PigWeightService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for PigWeight entities
 * 
 */

@Controller("PigWeightRestController")

public class PigWeightRestController {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages PigWeight entities
	 * 
	 */
	@Autowired
	private PigWeightDAO pigWeightDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for PigWeight entities
	 * 
	 */
	@Autowired
	private PigWeightService pigWeightService;

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Delete an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}/pig/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigWeightPig(@PathVariable Integer pigweight_id, @PathVariable Integer related_pig_id) {
		pigWeightService.deletePigWeightPig(pigweight_id, related_pig_id);
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}/pig/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadPigWeightPig(@PathVariable Integer pigweight_id, @PathVariable Integer related_pig_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		return pig;
	}

	/**
	* Show all PigWeight entities
	* 
	*/
	@RequestMapping(value = "/PigWeight", method = RequestMethod.GET)
	@ResponseBody
	public List<PigWeight> listPigWeights() {
		return new java.util.ArrayList<PigWeight>(pigWeightService.loadPigWeights());
	}

	/**
	* Save an existing PigWeight entity
	* 
	*/
	@RequestMapping(value = "/PigWeight", method = RequestMethod.PUT)
	@ResponseBody
	public PigWeight savePigWeight(@RequestBody PigWeight pigweight) {
		pigWeightService.savePigWeight(pigweight);
		return pigWeightDAO.findPigWeightByPrimaryKey(pigweight.getId());
	}

	/**
	* Create a new PigWeight entity
	* 
	*/
	@RequestMapping(value = "/PigWeight", method = RequestMethod.POST)
	@ResponseBody
	public PigWeight newPigWeight(@RequestBody PigWeight pigweight) {
		pigWeightService.savePigWeight(pigweight);
		return pigWeightDAO.findPigWeightByPrimaryKey(pigweight.getId());
	}

	/**
	* Select an existing PigWeight entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigWeight loadPigWeight(@PathVariable Integer pigweight_id) {
		return pigWeightDAO.findPigWeightByPrimaryKey(pigweight_id);
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}/pig", method = RequestMethod.PUT)
	@ResponseBody
	public Pig savePigWeightPig(@PathVariable Integer pigweight_id, @RequestBody Pig pig) {
		pigWeightService.savePigWeightPig(pigweight_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Create a new Pig entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}/pig", method = RequestMethod.POST)
	@ResponseBody
	public Pig newPigWeightPig(@PathVariable Integer pigweight_id, @RequestBody Pig pig) {
		pigWeightService.savePigWeightPig(pigweight_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Delete an existing PigWeight entity
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigWeight(@PathVariable Integer pigweight_id) {
		PigWeight pigweight = pigWeightDAO.findPigWeightByPrimaryKey(pigweight_id);
		pigWeightService.deletePigWeight(pigweight);
	}

	/**
	* Get Pig entity by PigWeight
	* 
	*/
	@RequestMapping(value = "/PigWeight/{pigweight_id}/pig", method = RequestMethod.GET)
	@ResponseBody
	public Pig getPigWeightPig(@PathVariable Integer pigweight_id) {
		return pigWeightDAO.findPigWeightByPrimaryKey(pigweight_id).getPig();
	}
}